/*
 * Chastoslov
 * Copyright (C) 2009 Alexandr Stroghov
 * 
 * stroghov@gmail.com
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the 
 * GNU General Public License, version 2, as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if 
 * not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 * MA 02111-1307 USA
*/

package world.ah.wf.guiclient;

import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import javax.swing.JOptionPane;

public class ErrorShower {
  //
  /**
   * Показывает сообщение об ошибке
   * @param parent 
   * @param action Действие, которое привело к ошибке
   * @param message Сообщение
   * @param exception возникшее исключение
   */
  public static void show(Component parent, String action, String message, Exception exception) {
    String msg = message.length() > 0 ? message : exception.getMessage();
    String details = action.length() > 0 ? General.getLangItem("Action", "Action") + ": " + 
      action + "\n" : "";
    details += msg + "\n" + exception.toString() + "\n";
    StackTraceElement[] stel = exception.getStackTrace();
    for(int i = 0; i < stel.length; i++) details += stel[i].toString() + "\n";
    //
    DialogError fr;
    if(parent instanceof Frame) fr = new DialogError((Frame)parent, "", true); 
    else if (parent instanceof Dialog) fr = new DialogError((Dialog)parent, "", true); 
    else fr = new DialogError((Frame)null, "", true); 
    fr.setErrorText(msg);
    fr.setDetails(details);
    fr.setVisible(true);
    //
    System.out.println("##### " + msg);
    exception.printStackTrace();
    //JOptionPane.showMessageDialog(parent, msg, "Error!", JOptionPane.ERROR_MESSAGE); 
  }
  //
  public static void show(Component parent, String message) {
    System.out.println(message);
    JOptionPane.showMessageDialog(parent, message, General.getLangItem("Error", "Error") + "!", 
      JOptionPane.ERROR_MESSAGE); 
  } 
  //
  //public static void show(String message) {show(null, message);}


}